package com.zhouheng.springbootactiviti.common.config;

import com.zhouheng.springbootactiviti.common.security.CustomAuthenticationProvider;
import com.zhouheng.springbootactiviti.common.security.JWTAuthenticationFilter;
import com.zhouheng.springbootactiviti.common.security.JWTLoginFilter;
import com.zhouheng.springbootactiviti.module.mapper.SysMenuMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.servlet.HandlerExceptionResolver;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserDetailsService userDetailsService;

    private final SysMenuMapper sysMenuMapper;

    private final HandlerExceptionResolver handlerExceptionResolver;

    public WebSecurityConfig(UserDetailsService userDetailsService, SysMenuMapper sysMenuMapper, HandlerExceptionResolver handlerExceptionResolver) {
        this.userDetailsService = userDetailsService;
        this.sysMenuMapper = sysMenuMapper;
        this.handlerExceptionResolver = handlerExceptionResolver;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(AUTH_WHITELIST);
    }

    private static final String[] AUTH_WHITELIST = {
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**",
            "/doc.html"
    };

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/sys/user/signUp").permitAll()
                .antMatchers(HttpMethod.GET, "/sys/user/captcha").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTLoginFilter(authenticationManager(), handlerExceptionResolver))
                .addFilter(new JWTAuthenticationFilter(authenticationManager(), sysMenuMapper));
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(new CustomAuthenticationProvider(userDetailsService));
    }

}
